﻿# ZenCoding Visual Studio AddIn
# Copyright (C) 2009 Einar Egilsson
# http://tech.einaregilsson.com/2009/11/12/zen-coding-visual-studio-addin/
# 
# Portions of this program (the ZenCoding Python library) were taken from
# the ZenCoding project (http://code.google.com/p/zen-coding/)
# 
# Those parts are copyright (C) 2009 Sergey Chikuyonok (http://chikuyonok.ru)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
# $Id: settings.py 333 2009-12-02 08:02:43Z einar@einaregilsson.com $ 

from System.IO import Path, File
from System import Environment

SETTINGS_FILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 'my_zen_settings.py')

if not File.Exists(SETTINGS_FILE):
	File.WriteAllText(SETTINGS_FILE, 'my_zen_settings = {}');

def load_custom_settings():
	context = {}
	#execfile forces the file to be really read from disk
	execfile(SETTINGS_FILE, context)
	return context['my_zen_settings']
	
def save_custom_settings(settings):
	f = open(SETTINGS_FILE, 'w')
	f.write('# This file is auto-generated by the ZenCoding VisualStudio plugin\n')
	f.write('# Do not make changes directly in this file, they will be overwritten!\n\n\n')
	f.write('my_zen_settings = ')
	
	#Try to prettify, however if there's a bug in the prettify code
	#we don't want it to ruin anything
	try:
		ps = pretty(settings)
		eval(ps, {},{})
		f.write(ps)
	except:
		f.write(repr(settings))
	f.close()	
							 
def pretty(hash,indent=1):
	s = '{\n'

	ordered = [k for k in hash]
	ordered.sort()	
	prefix = indent*'\t'
	for key in ordered:
		s += prefix + "%s : " % repr(key)
		if type(hash[key]) == dict:
			s += pretty(hash[key], indent+1)
		else:
			value = repr(hash[key])
			if not '\\n' in value:
				s += value
			else:
				if value[0] == '"':
					q = '"'
				else:
					q = "'"
				lines = value.split('\\n')
				sep = '\\n%s + \n%s%s' % (q, prefix + (' '*(len(key)+5)),q)
				s += sep.join(lines)
		s += ',\n'
				
	s += '\t'*(indent-1) + '}'
	return s

